package com.lankystar.logistucs.spring.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lankystar.logistucs.spring.entity.TwoOrder;
import com.lankystar.logistucs.spring.entity.User;
import com.lankystar.logistucs.spring.entity.UserType;
import com.lankystar.logistucs.spring.service.MobileService;
import com.lankystar.logistucs.spring.service.OneOrderService;
import com.lankystar.logistucs.spring.service.OrderService;
import com.lankystar.logistucs.spring.service.TwoOrderService;
import com.lankystar.logistucs.spring.service.UserService;

@Controller
@RequestMapping(value="/mobile")
public class MobileUserController {
/**
 * 用户
 */
	Map<String, Object> map = new HashMap<>();
	@Autowired
	private UserService userService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private MobileService mobileService;
	@Autowired
	private OneOrderService oneOrderService;
	@Autowired
	private TwoOrderService twoOrderService;
	
	
	@RequestMapping(value="/login.do")
	@ResponseBody
	public Map<String, Object> login(User record){
		try {
			User user = userService.login(record.getUserPhone(), record.getUserPassword());
			UserType userType = user.getUserType();
			Map<String, String> data = new HashMap<>();
			data.put("userId", String.valueOf(user.getUserId()));
			data.put("userPhone", user.getUserPhone());
			data.put("userName", user.getUserName());
			data.put("userType", String.valueOf(userType.getUserKey()));
			data.put("userTypeName", userType.getUserValue());
			map.put("data", data);
			map.put("msg", "用户登录成功！！");
			map.put("code", 1);
		} catch (Exception e) {
			map.put("msg", "用户登录失败,用户不存在！！");
			map.put("code", 0);
		}
		return map;
	}
	/**
	 * 司机更新一级订单状态
	 * @param userId
	 * @param oneStatusKey
	 * @return
	 */
	@RequestMapping(value="/updateOneOrder.do")
	@ResponseBody
	public Map<String, Object> updateOneOrder(int userId , int oneStatusKey){
		try {
			User user = userService.selectUserById(userId);
			Map<String, Object> data = orderService.updateOneOrder(user, oneStatusKey);
			map.put("data", data);
			map.put("code", 1);
			map.put("msg", "司机更新一级订单状态成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "司机更新一级订单状态失败！！");
		}
		return map;
	}
	
	/**
	 * 司机查询订单 oneStatusKey != 70
	 * @param userId
	 * @param createTime
	 * @return
	 */
	@RequestMapping(value="/queryOrder.do")
	@ResponseBody
	public Map<String, Object> queryOrder(int userId){
		try {
			map.put("data", mobileService.queryOneOrderAndOneStatus(userId));
			map.put("code", 1);
			map.put("msg", "司机查询订单成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "司机查询订单失败！！");
		}
		return map;
	}
	/**
	 * 上海报关行,苏州报关行查询订单、订单状态(支持总提单号、分单号查询)
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/queryOrderlist.do")
	@ResponseBody
	public Map<String, Object> queryOrderlist(String totalOrderNu, String pointOrderNu,int userId){
		try {
			map.put("data", oneOrderService.selectOrderAndStatus(totalOrderNu, pointOrderNu, userId));
			map.put("code", 1);
			map.put("msg", "查询订单成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "查询订单失败！！");
		}
		return map;
	}
	
	/**
	 * 上海报关行更新订单
	 * @param twoOrderId
	 * @param twoStatusKey
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/updateTwoOrder.do")
	@ResponseBody
	public Map<String, Object> updateTwoOrder(int oneOrderId, int twoStatusKey,int userId){
		try {
			List<TwoOrder> twoOrderlist = twoOrderService.queryTwoOrderByOneOrderId(oneOrderId);
			for (TwoOrder to : twoOrderlist) {
				Object[] data = mobileService.updateTwoOrder(to.getTwoOrderId(), twoStatusKey, userId);
				map.put("data", data);
			}
			map.put("code", 1);
			map.put("msg", "订单状态成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "订单状态失败！！");
		}
		return map;
	}
	
	
	
}
